草庐IT

C++ ifstream 到 char *

全部标签

c++ - ifstream.eof() - 在实际结束之前到达文件末尾

我有一个大约11.1G的二进制文件,其中存储了一系列来自Kinect的深度帧。此文件中有19437帧。为了每次读取一帧,我在fstream中使用了ifstream但它达到了eof在文件真正结束之前。(我只得到了前20帧,由于eof标志,函数停止了)但是,可以使用stdio中的fread读取所有帧。谁能解释一下这种情况?感谢您抽出宝贵时间回答我的问题。这是我的两个函数://ifstream.read()-DoesNotWork:theloopwillstopafter20thframebecauseoftheeofflagifstreamdepthStream("fileName.dat

C++ 错误 : Incompatible types in assignment of ‘char*’ to ‘char [2]

我的构造函数有点问题。在我的头文件中,我声明:charshort_name_[2];和其他变量在我的构造函数中:Territory(std::stringname,charshort_name[2],Player*owner,charunits);voidsetShortName(char*short_name);inlineconstchar(&getShortName()const)[2]{returnshort_name_;}在我的cpp文件中:Territory::Territory(std::stringname,charshort_name[2],Player*owner,

c++ - 编译器如何知道字符串文字(const char*)已经存在于数据存储器中?

我知道了charszA[]="abc";使用“abc”初始化数组szA,所以存放在栈内存中,函数结束时销毁。另一方面,考虑:char*szB="abc";这里的“abc”像静态变量一样存放在数据内存段,szB只是它的一个地址。在这一点上,我很奇怪:如果我尝试inti=0;while(i++这会在数据部分产生1000000个“hello”吗?为了解决这个问题,我编写了测试代码:#includeusingnamespacestd;char*testA(char*arr){returnarr;}char*testB(char*arr){returnarr;}voidmain(){cout结果

c++ - const char * 对 vector<unsigned char> 的初始化

我知道在使用C++和STL时,使用vector是存储二进制数据的好方法。但是,对于我的单元测试,我想使用constchar*C字符串变量来初始化vector。我正在尝试使用此处找到的代码变体-Converting(void*)tostd::vector-这样做:constchar*testdata="thequickbrownfoxjumpsoverthelazydog.";unsignedchar*buffer=(unsignedchar*)testdata;typedefvectorbufferType;bufferType::size_typesize=strlen((const

c++ - 操作数类型不兼容 ("char"和 "const char*")

我收到以下错误...Operandtypesareincompatible("char"and"constchar*")...尝试执行if语句时。我假设我不了解输入值的存储方式,尽管我不确定是否可以将其转换为匹配类型?要重现的示例代码是:charuserInput_Text[3];if(userInput_Text[1]=="y"){//Dostuff.}我不确定是什么原因造成的。看起来一种类型是char而另一种是constchar指针,尽管我不确定是什么,作为引用,当我不使用数组时也会发生此错误)。非常感谢提示/反馈。 最佳答案

c++ - Gcc:强制编译器默认使用 unsigned char

由于当unsigned限定符不存在时,C++中char的性质是编译器相关的,是否有一个参数我可以传递给GCC强制将所有char编译为unsigned? 最佳答案 您正在寻找的标志是-funsigned-char。来自thedocumentation:-funsigned-charLetthetypecharbeunsigned,likeunsignedchar.Eachkindofmachinehasadefaultforwhatcharshouldbe.Itiseitherlikeunsignedcharbydefaultorli

C++ "error: passing ' const std::map<int, std::basic_string<char>>' as ' this' argument of ..."

使用以下代码(为简洁起见摘录):颜色.h:classcolor{public:color();enumcolorType{black,blue,green,cyan,red,magenta,brown,lightgray,nocolor};colorTypegetColorType();voidsetColorType(colorTypecColortype);stringgetColorText()const;private:colorTypecColortype=nocolor;mapcolors={{black,"black"},{blue,"blue"},{green,"gre

c++ - C 或 C++。如何比较给定 char * 指针的两个字符串?

我正在以两种方式对我的汽车阵列进行排序。如下所示。和另一个制造商。Make是一个char*当我只有指向它们的指针时如何比较字符串?inti,j;for(i=0;iyear>carArray[j+1]->year){swap(carArray[j],carArray[j+1]);}}}}上述方法适用于int的(年)。我怎样才能使它适用于字符指针? 最佳答案 在几乎任何一种情况下,方式都是调用strcmp.如果你的字符串(出于某种奇怪的原因)没有以NUL结尾,你应该使用strncmp相反。但是,在C++中,如果可以合理避免的话,您真的不

以 char* 为键的 C++ unordered_map

尝试使用以char*为键的容器unordered_map时,我感到筋疲力尽(在Windows上,我使用的是VS2010)。我知道我必须为char*定义我自己的比较函数,它继承自binary_function。以下是示例程序。#include#include#includeusingnamespacestd;templatestructmy_equal_to:publicbinary_function{booloperator()(const_Tp&__x,const_Tp&__y)const{returnstrcmp(__x,__y)==0;}};typedefunordered_ma

c++ - 通过指向 int 的指针为 char 数组起别名合法吗?

我知道标准中明确允许以下内容:intn=0;char*ptr=(char*)&n;cout这个呢?alignas(int)charstorage[sizeof(int)];int*ptr=(int*)&storage[0];*ptr=0;cout本质上,我是在问别名规则是否允许通过指向另一种类型的指针访问一系列字符。如果可能,我希望引用标准中指示某种方式的部分。标准的某些部分让我感到矛盾;(3.10.10)似乎表明它是未定义的行为,前提是storage的动态类型不是int。但是,我不清楚动态类型的定义,std::aligned_storage的存在让我相信这是是可能的。